package com.codegeek.project.system.controller;

import com.codegeek.common.utils.SecurityUtils;
import com.codegeek.common.utils.ServletUtils;
import com.codegeek.framework.aspectj.lang.annotation.Log;
import com.codegeek.framework.aspectj.lang.enums.BusinessType;
import com.codegeek.framework.security.LoginUser;
import com.codegeek.framework.security.service.TokenService;
import com.codegeek.framework.web.controller.BaseController;
import com.codegeek.framework.web.domain.AjaxResult;
import com.codegeek.project.common.Decode;
import com.codegeek.project.system.domain.SysUser;
import com.codegeek.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 个人信息 业务处理
 *
 * @author codegeek
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 个人信息
     */
    @GetMapping
    public AjaxResult profile()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        AjaxResult ajax = AjaxResult.success(user);
        ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
        ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
        return ajax;
    }

    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping
    public AjaxResult updateProfile(@RequestBody SysUser user)
    {
        return toAjax(userService.updateUserProfile(user));
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePwd")
    @PreAuthorize("@ss.hasPermi('system:user:profile:updatePwd')")
    public AjaxResult updatePwd(String oldPassword, String newPassword) throws Exception {
        oldPassword = oldPassword.replace("$","+");
        newPassword = newPassword.replace("$","+");
        String oldP = Decode.getValue(oldPassword);
        String newP = Decode.getValue(newPassword);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldP, password))
        {
            return AjaxResult.error("修改密码失败，旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newP, password))
        {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        return toAjax(userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newP)));
    }

    private final String goBack = "../";
    private final String blank = " ";

    /**
     * 头像上传
     */
//    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
//    @PostMapping("/avatar")
//    @PreAuthorize("@ss.hasPermi('system:user:profile:avatar')")
//    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
//    {
//        if (!file.isEmpty())
//        {
//            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//            String filePath = RuoYiConfig.getAvatarPath();
//            if (filePath.indexOf(goBack) != -1 || filePath.indexOf(blank) != -1) {
//                throw new IOException("文件路径非法，不允许存在../或者空格");
//            }
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
//            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
//            {
//                AjaxResult ajax = AjaxResult.success();
//                ajax.put("imgUrl", avatar);
//                loginUser.getUser().setAvatar(avatar);
//                tokenService.setLoginUser(loginUser);
//                return ajax;
//            }
//        }
//        return AjaxResult.error("上传图片异常，请联系管理员");
//    }
}
